package com.ross.web.dao;

import java.util.LinkedList;
import java.util.List;

import com.ross.web.domain.AuditLog;
import com.ross.web.domain.Users;
import com.ross.web.util.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

public class AuditLogDAO extends HibernateUtil {
	
	public List<AuditLog> getAllAuditLogs(){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(AuditLog.class,"al")
					.setProjection(Projections.projectionList()
							.add(Projections.property("al.al_id"),"al_id")
							.add(Projections.property("al.al_log_date"),"al_log_date")
							.add(Projections.property("al.al_log_time"),"al_log_time")
							.add(Projections.property("al.al_products"),"al_products")
							.add(Projections.property("al.al_grand_total"),"al_grand_total")
							.add(Projections.property("al.al_user"),"al_user")
							.add(Projections.property("al.al_username"),"al_username")
							)
							
							.setResultTransformer(new AliasToBeanResultTransformer(AuditLog.class))
							.list();
			
			return result;
			
		}finally{
			session.close();
		}
	}
	
	public AuditLog getAuditLogById(int al_id){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(AuditLog.class,"al")
					.setProjection(Projections.projectionList()
							.add(Projections.property("al.al_id"),"al_id")
							.add(Projections.property("al.al_log_date"),"al_log_date")
							.add(Projections.property("al.al_log_time"),"al_log_time")
							.add(Projections.property("al.al_products"),"al_products")
							.add(Projections.property("al.al_grand_total"),"al_grand_total")
							.add(Projections.property("al.al_user"),"al_user")
							.add(Projections.property("al.al_username"),"al_username")
							)
							
							.add(Restrictions.eq("al.al_id", al_id))
							
							.setResultTransformer(new AliasToBeanResultTransformer(AuditLog.class))
							.list();
			
			return result.isEmpty() ? null : (AuditLog) result.get(0);
			
		}finally{
			session.close();
		}
	}
	
	public List<AuditLog> getAllAuditLogsByLogUserLogDate(String logDate, String username){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		Criterion cRestric = null;
		if(username.equals("all")){
			cRestric = Restrictions.eq("al.al_log_date", logDate);
		}else{
			cRestric = Restrictions.and(Restrictions.eq("al.al_log_date", logDate), 
					Restrictions.eq("al.al_username", username));
		}
		try{
			
			result = session.createCriteria(AuditLog.class,"al")
					.setProjection(Projections.projectionList()
							.add(Projections.property("al.al_id"),"al_id")
							.add(Projections.property("al.al_log_date"),"al_log_date")
							.add(Projections.property("al.al_log_time"),"al_log_time")
							.add(Projections.property("al.al_products"),"al_products")
							.add(Projections.property("al.al_grand_total"),"al_grand_total")
							.add(Projections.property("al.al_user"),"al_user")
							.add(Projections.property("al.al_username"),"al_username")
							)
							
							.add(cRestric)
							
							.setResultTransformer(new AliasToBeanResultTransformer(AuditLog.class))
							.list();
			
			return result;
			
		}finally{
			session.close();
		}
	}
	
	public void insert(AuditLog auditlog) throws Exception{
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.save(auditlog);
			tx.commit();
		}catch (Exception e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
	
	public void update(AuditLog auditlog) throws Exception{
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.update(auditlog);
			tx.commit();
		}catch (Exception e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
	
	public void delete(AuditLog auditlog) throws Exception{
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.delete(auditlog);
			tx.commit();
		}catch (Exception e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}finally{
			session.close();
		}
	}

}
